/*
 * LoadGame.java
 *
 * Created on Apr 22, 2010, 1:31:29 PM
 */

package controller.permainan;

import helper.*;
import model.board.*;
import model.players.*;

public class Trade_dialog extends javax.swing.JFrame{

    private Permainan2 parent;
    private Player trader;
    private Player player;
    int urutan_player;
    private int traderpos_idx; //posisi kotak punya nya trader
    private int playerpos_idx; //posisi kotak punyanya player
    String[] trader_asetlist;
    String[] player_asetlist;
    public static int count = 0;

    /** Creates new form LoadGame */
    public Trade_dialog(Permainan2 _parent, java.awt.event.MouseEvent trader_evt, java.awt.event.MouseEvent player_evt, int urutan_player)
    {
        initComponents();
        parent = _parent;
        trader = parent.G.current_plyr();
        this.urutan_player = urutan_player;
        player = parent.G.Q.player[urutan_player];
        this.setLocationRelativeTo(null);

        //isi list aset trader & player
        LoadTraderAssetList();
        LoadPlayerAssetList();
        //isi nama & icon
        tradername_label.setText(trader.getNama());
        tradersmallpic_label.setIcon(trader.getSmallImage());
        playername_label.setText(player.getNama());
        playersmallpic_label.setIcon(player.getSmallImage());

        //otomatis select aset trader yang tadi diklik
        if (trader_evt != null)
        {
            javax.swing.JPanel source = (javax.swing.JPanel) trader_evt.getSource();
            int i = 1;
            while ((i<=32)&& (source != parent.kotak_panel[i]))
            {
                i++;
            }
            if (source == parent.kotak_panel[i])
            {
                traderaset_list.setSelectedValue(parent.kotakname_textfield[i].getText(), true);
            }
        }
        //otomatis select aset player yang tadi diklik sama trader
        if (player_evt != null)
        {
            javax.swing.JPanel source = (javax.swing.JPanel) player_evt.getSource();
            int i = 1;
            while ((i<=32)&& (source != parent.kotak_panel[i]))
            {
                i++;
            }
            if (source == parent.kotak_panel[i])
            {
                playeraset_list.setSelectedValue(parent.kotakname_textfield[i].getText(), true);
            }
        }
    }



    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        back_button = new javax.swing.JButton();
        trade_button = new javax.swing.JButton();
        plyrattr_panel = new javax.swing.JPanel();
        traderaset_pane = new javax.swing.JScrollPane();
        traderaset_list = new javax.swing.JList();
        tradersmallpic_label = new javax.swing.JLabel();
        tradername_label = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        asetpic_label = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        asetattr_textarea = new javax.swing.JTextArea();
        plyrattr_panel1 = new javax.swing.JPanel();
        playeraset_pane = new javax.swing.JScrollPane();
        playeraset_list = new javax.swing.JList();
        playersmallpic_label = new javax.swing.JLabel();
        playername_label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Load Game");

        back_button.setText("<< Back");
        back_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                back_buttonActionPerformed(evt);
            }
        });

        trade_button.setText("Trade!");
        trade_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trade_buttonActionPerformed(evt);
            }
        });

        plyrattr_panel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        traderaset_list.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        traderaset_list.setFont(new java.awt.Font("Hand Me Down S (BRK)", 0, 20));
        traderaset_list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        traderaset_list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                traderaset_listValueChanged(evt);
            }
        });
        traderaset_pane.setViewportView(traderaset_list);

        tradersmallpic_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        tradersmallpic_label.setText("tradersmallpic");

        tradername_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        tradername_label.setText("tradername");

        javax.swing.GroupLayout plyrattr_panelLayout = new javax.swing.GroupLayout(plyrattr_panel);
        plyrattr_panel.setLayout(plyrattr_panelLayout);
        plyrattr_panelLayout.setHorizontalGroup(
            plyrattr_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(plyrattr_panelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(plyrattr_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(traderaset_pane, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, plyrattr_panelLayout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(tradersmallpic_label, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tradername_label, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        plyrattr_panelLayout.setVerticalGroup(
            plyrattr_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(plyrattr_panelLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(plyrattr_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tradersmallpic_label, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tradername_label, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(traderaset_pane, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED, null, java.awt.Color.white, null, null));

        asetpic_label.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
        asetpic_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        asetattr_textarea.setColumns(20);
        asetattr_textarea.setRows(5);
        jScrollPane1.setViewportView(asetattr_textarea);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(asetpic_label, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(asetpic_label, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        plyrattr_panel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        playeraset_list.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        playeraset_list.setFont(new java.awt.Font("Hand Me Down S (BRK)", 0, 20));
        playeraset_list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        playeraset_list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                playeraset_listValueChanged(evt);
            }
        });
        playeraset_pane.setViewportView(playeraset_list);

        playersmallpic_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        playersmallpic_label.setText("smallpic");

        playername_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        playername_label.setText("plyr_name");

        javax.swing.GroupLayout plyrattr_panel1Layout = new javax.swing.GroupLayout(plyrattr_panel1);
        plyrattr_panel1.setLayout(plyrattr_panel1Layout);
        plyrattr_panel1Layout.setHorizontalGroup(
            plyrattr_panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(plyrattr_panel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(plyrattr_panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(playeraset_pane, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, plyrattr_panel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(playersmallpic_label, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(playername_label, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        plyrattr_panel1Layout.setVerticalGroup(
            plyrattr_panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(plyrattr_panel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(plyrattr_panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(playername_label, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(playersmallpic_label, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(playeraset_pane, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(back_button, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(plyrattr_panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(plyrattr_panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(trade_button, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(plyrattr_panel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(plyrattr_panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(back_button, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(trade_button, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(72, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LoadTraderAssetList()
    {
        trader_asetlist = new String[trader.jum_aset];
        for (int i =1; i<=trader.jum_aset; i++)
        {
            trader_asetlist[i-1] = parent.G.B[trader.aset[i]].getNama();
        }

        traderaset_list.setModel(new javax.swing.AbstractListModel()
        {

            public int getSize()
            {
                return trader_asetlist.length;
            }

            public Object getElementAt(int index)
            {
                return trader_asetlist[index];
            }
        });
        this.traderaset_pane.setViewportView(this.traderaset_list);
        this.traderaset_pane.repaint();
       
    }

    private void LoadPlayerAssetList()
    {
        player_asetlist = new String[player.jum_aset];
        for (int i =1; i<=player.jum_aset; i++)
        {
            player_asetlist[i-1] = parent.G.B[player.aset[i]].getNama();
        }

        playeraset_list.setModel(new javax.swing.AbstractListModel()
        {

            public int getSize()
            {
                return player_asetlist.length;
            }

            public Object getElementAt(int index)
            {
                return player_asetlist[index];
            }
        });
        this.playeraset_pane.setViewportView(this.playeraset_list);
        this.playeraset_pane.repaint();

    }

    

    private void back_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_back_buttonActionPerformed
        // TODO add your handling code here:
        this.dispose();

        parent.ShowPlyrAttr(parent.G.Q.getUrutan(player));
        parent.ShowPlyrAttr(parent.G.urutan_current_plyr);
        parent.ShowAssetThesePlyrs(parent.G.urutan_current_plyr, parent.G.Q.getUrutan(player));
        parent.trd_button.setText("Trade");
        parent.act_panel.setVisible(true);
        parent.setVisible(true);
    }//GEN-LAST:event_back_buttonActionPerformed

    private void traderaset_listValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_traderaset_listValueChanged
        String nama = traderaset_list.getSelectedValue().toString();
        //asetpic_label.setText(nama);
        int idx = helper.SearchEngine.LokasionBoard(parent.G.B, traderaset_list.getSelectedValue().toString());
        asetattr_textarea.setText(parent.G.B[idx].PrintAttribute());
        asetpic_label.setIcon(SearchEngine.createImageIcon("iconLP"+idx+".jpg"));


}//GEN-LAST:event_traderaset_listValueChanged

    private void trade_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trade_buttonActionPerformed
        traderpos_idx = helper.SearchEngine.LokasionBoard(parent.G.B, traderaset_list.getSelectedValue().toString());
        Kotak traderKotak = parent.G.B[traderpos_idx];
        int traderaset_idx = helper.SearchEngine.WhereThisLPisinYourAset(traderpos_idx, trader);

        playerpos_idx = helper.SearchEngine.LokasionBoard(parent.G.B, playeraset_list.getSelectedValue().toString());
        Kotak playerKotak = parent.G.B[playerpos_idx];
        int playeraset_idx = helper.SearchEngine.WhereThisLPisinYourAset(playerpos_idx, player);

        //ganti dari attr pemain
        int temp = trader.aset[traderaset_idx];
        trader.aset[traderaset_idx] = player.aset[playeraset_idx];
        player.aset[playeraset_idx] = temp;
        //ganti di asetnya
        traderKotak.setOwner(player);
        playerKotak.setOwner(trader);
        this.dispose();
        javax.swing.JOptionPane.showMessageDialog(this, "Successfully traded~");
        parent.HistoryWrite(trader.getNama()+" trades "+traderKotak.getNama()+" with "+playerKotak.getNama()+" with "+player.getNama());
        parent.AssignAsThisPlyr(parent.G.urutan_current_plyr);
        
        parent.trd_button.setText("Trade");
        parent.act_panel.setVisible(true);
        parent.setVisible(true);

        
}//GEN-LAST:event_trade_buttonActionPerformed

    private void playeraset_listValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_playeraset_listValueChanged
        String nama = playeraset_list.getSelectedValue().toString();
        //asetpic_label.setText(nama);
        int idx = helper.SearchEngine.LokasionBoard(parent.G.B, playeraset_list.getSelectedValue().toString());
        asetattr_textarea.setText(parent.G.B[idx].PrintAttribute());
        asetpic_label.setIcon(SearchEngine.createImageIcon("iconLP"+idx+".jpg"));
}//GEN-LAST:event_playeraset_listValueChanged

     public static void puts (String S)
    {
        System.out.println(S);
    }
     public static void puts (int S)
    {
        System.out.println(S);
    }
     
    public static void main(String args[]) {

        final Permainan2 game = new Permainan2();
        game.G.Q.player[1].aset[1] = 2;
        game.G.Q.player[1].aset[2] = 3;
        game.G.Q.player[1].aset[3] = 10;
        game.G.Q.player[1].jum_aset = 3;
        game.G.Q.player[2].aset[1] = 11;
        game.G.Q.player[2].jum_aset = 1;
        game.G.urutan_current_plyr = 1;
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Trade_dialog(game, null, null, 2).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea asetattr_textarea;
    private javax.swing.JLabel asetpic_label;
    private javax.swing.JButton back_button;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList playeraset_list;
    private javax.swing.JScrollPane playeraset_pane;
    private javax.swing.JLabel playername_label;
    private javax.swing.JLabel playersmallpic_label;
    private javax.swing.JPanel plyrattr_panel;
    private javax.swing.JPanel plyrattr_panel1;
    private javax.swing.JButton trade_button;
    private javax.swing.JList traderaset_list;
    private javax.swing.JScrollPane traderaset_pane;
    private javax.swing.JLabel tradername_label;
    private javax.swing.JLabel tradersmallpic_label;
    // End of variables declaration//GEN-END:variables

}
